/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Component;

import GameObject.ImageObject;
import GameObject.StaticImage;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 *
 * @author Huy
 */
public class MyPanel extends MyControlItem {

	private ArrayList<MyControlItem> items;

	public MyPanel(BufferedImage img, int x, int y) {
		items = new ArrayList<MyControlItem>();
		sprite = new StaticImage(img, x, y);
	}

	public MyPanel(BufferedImage img, int w, int h, int x, int y) {
		items = new ArrayList<MyControlItem>();
		sprite = new StaticImage(img, w, h, x, y);
	}

	public MyPanel(int w, int h, Color c, int x, int y) {
		items = new ArrayList<MyControlItem>();
		sprite = new StaticImage(panelImage(w, h, c), x, y);

	}

	public BufferedImage getImage() {
		return sprite.getImage();
	}

	private BufferedImage panelImage(int w, int h, Color c) {
		BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = (Graphics2D) img.getGraphics();
		RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(0, 0, w - 1, h - 1, 10, 10);
		g.setColor(c);
		g.fill(roundedRectangle);
		g.setColor(c.darker());
		g.setStroke(new BasicStroke(3));
		g.draw(roundedRectangle);
		return img;
	}

	public void add(MyControlItem ctr) {
		ctr.setScaleX(sprite.getScaleX());
		ctr.setScaleY(sprite.getScaleY());
		ctr.move(sprite.getPosition().x, sprite.getPosition().y);
		items.add(ctr);
	}

	public void remove(MyControlItem ctr) {
		items.remove(ctr);
	}

	public void clear() {
		items.clear();
	}

	@Override
	public void setScaleX(float s) {
		sprite.setScaleX(s);
		for (MyControlItem myControlItem : items) {
			myControlItem.setScaleX(s);
		}
	}
	@Override
	public void setScaleY(float s) {
		sprite.setScaleY(s);
		for (MyControlItem myControlItem : items) {
			myControlItem.setScaleY(s);
		}
	}


	@Override
	public void draw(Graphics2D g) {
		if (!isVisible) {
			return;
		}
		sprite.draw(g);
		for (int i = items.size() - 1; i >= 0; i--) {
			items.get(i).draw(g);
		}
	}

	@Override
	public void move(int dx, int dy) {
		sprite.move(dx, dy);
		for (MyControlItem myControlItem : items) {
			myControlItem.move(dx, dy);
		}
	}

	@Override
	public boolean keyPressed(KeyEvent e) {
		if (!isEnable || !isVisible) {
			return false;
		}
		if (isForcus) {
			return super.keyPressed(e);
		}
		boolean flag = false;
		for (MyControlItem myControlItem : items) {
			if (myControlItem.keyPressed(e)) {
				flag = true;
			}
		}
		return flag;
	}

	@Override
	public boolean keyReleased(KeyEvent e) {
		if (!isEnable || !isVisible) {
			return false;
		}
		if (isForcus) {
			return super.keyReleased(e);
		}
		boolean flag = false;
		for (MyControlItem myControlItem : items) {
			if (myControlItem.keyReleased(e)) {
				flag = true;
			}
		}
		return flag;
	}

	@Override
	public boolean mouseMoved(MouseEvent e) {
		if (e == null || !isEnable || !isVisible) {
			return false;
		}
		boolean flag = false;
		for (MyControlItem myControlItem : items) {
			if (flag) {
				myControlItem.mouseMoved(null);
			} else if (myControlItem.mouseMoved(e)) {
				flag = true;
			}
		}
		if (!flag) {
			return super.mouseMoved(e);
		}
		return flag;
	}

	@Override
	public boolean mousePressed(MouseEvent e) {
		if (!isEnable || !isVisible) {
			return false;
		}
		for (MyControlItem myControlItem : items) {
			if (myControlItem.mousePressed(e)) {
				return true;
			}
		}

		if(ImageObject.isIn(e.getPoint(), sprite.getBound())) {
			isMousePressed = true;
			if (mouseHandler != null) {
				mouseHandler.mousePressed(e);
			}
			return true;
		}
		return false;

	}

	@Override
	public boolean mouseReleased(MouseEvent e) {
		if (e == null || !isEnable || !isVisible) {
			return false;
		}
		boolean flag = false;
		for (MyControlItem myControlItem : items) {
			if (flag) {
				myControlItem.mouseReleased(null);
			} else if (myControlItem.mouseReleased(e)) {
				flag = true;
			}
		}
		if (flag) {
			isForcus = false;
			return true;
		}
		if (isMousePressed && ImageObject.isIn(e.getPoint(), sprite.getBound())) {

			isForcus = true;
			isMousePressed = false;
			if (mouseHandler != null) {
				mouseHandler.mouseReleased(e);
			}
			return true;
		}
		isForcus = false;
		return false;

	}
}
